package com.spdg.ring.bo;

import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import cn.wolf.http.HttpCallBack;
import cn.wolf.notify.NotificationConstants;
import cn.wolf.notify.NotificationEntity;
import cn.wolf.notify.NotificationEntity.Message;
import cn.wolf.push.PushManager;
import cn.wolf.push.PushUtil;
import cn.wolf.tools.Base64Util;

import com.spdg.ring.PushArticleActivity;
import com.spdg.ring.R;
import com.spdg.ring.receiver.PushReceiver;
import com.spdg.ring.resp.PushResp;
import com.spdg.ring.task.PushTask;

/**
 * <p>Title: 推送逻辑类       </p>
 * <p>Description: 
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-11-15             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class PushBo implements HttpCallBack<PushResp> {

	private Context mContext;

	public PushBo(Context context) {
		this.mContext = context;
	}

	public void startPush() {
		PushTask task = new PushTask(this, mContext);
		task.execute();
	}

	public void startAlarm() {
		PushManager.startPush(mContext, PushReceiver.class);
	}

	public void stopAlarm() {
		PushManager.stopPush(mContext, PushReceiver.class);
	}

	@Override
	public void call(PushResp resp) {
		if (resp.isSuccess()) {
			NotificationEntity entity = resp.getmNotice();
			if (entity == null)
				return;
			List<Message> noticeList = entity.getContent();
			if (noticeList == null)
				return;
			for (Iterator<Message> it = noticeList.iterator(); it.hasNext();) {
				Message message = it.next();
				String title = Base64Util.decode2String(message.getTitle());
//				String content = Base64Util.decode2String(message.getMessage());
				String content = "";

				Intent intent = new Intent();
				intent.setAction(cn.wolf.notify.NotificationReceiver.NOTIFICATION_SHOW_ACTION);
				intent.putExtra(NotificationConstants.NOTIFICATION_TITLE, title);
				intent.putExtra(NotificationConstants.NOTIFICATION_CONTENT, content);
				intent.putExtra(NotificationConstants.NOTIFICATION_MESSAGE, message);
				intent.putExtra(NotificationConstants.NOTIFICATION_ICON, R.drawable.ic_launcher); // 增加默认推送图标

				// 状态控制
				boolean sound = PushUtil.getPushSoundEnabled(mContext);
				intent.putExtra(NotificationConstants.SETTINGS_SOUND_ENABLED, sound);
				boolean vibrate = PushUtil.getPushVibrateEnabled(mContext);
				intent.putExtra(NotificationConstants.SETTINGS_VIBRATE_ENABLED, vibrate);
				boolean pushEnabled = PushUtil.getPushEnabled(mContext);
				intent.putExtra(NotificationConstants.SETTINGS_NOTIFICATION_ENABLED, pushEnabled);

				String className = PushArticleActivity.class.getName(); // 跳转类名

				intent.putExtra(NotificationConstants.NOTIFICATION_CLASS, className);

				// 在状态栏进行通知展示
				mContext.sendBroadcast(intent);
			}
		}
	}

	@Override
	public void progress(Object... obj) {
	}

	@Override
	public void onNetWorkError() {
		
	}
}
